<!DOCTYPE html>
<%@ page errorPage="error/Error.jsp" contentType="text/html;charset=UTF-8"  import="com.mmp.common.util.*,com.mmp.main.entity.*,java.util.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>متابعة أمر عمل مفتوح</title>
</head>
<body>
<form action="JobOrderTrackingProcess" method="post" enctype="multipart/form-data" name="form1">
  <table class="layout_grid">
    <tr>
      <td colspan="2"><h1 class="page_title">شاشة متابعة أمر عمل مفتوح</h1></td>
    </tr>
    <tr>
      <td colspan="2"><%@ include file="../DeviceAndJobOrderInfo.jsp" %></td>
    </tr>
    <tr>
      <td class="side_label_top">الإجراء الأول :</td>
      <td><textarea name="firstAction" id="firstAction" required></textarea></td>
    </tr>
    <tr>
      <td class="side_label_middle">تاريخ الإجراء الأول :</td>
      <td><input type="text" name="firstActionDate" id="firstActionDate" class="caldr" required></td>
    </tr>
    <tr>
      <td class="side_label_middle">الإصلاح بقطع غيار :</td>
      <td><input name="fixIncludingSpareParts" value="1" type="radio">
        نعم
        <input name="fixIncludingSpareParts" value="0" type="radio">
        لا </td>
    </tr>
    <tbody id="grp1">
      <tr>
        <td class="side_label_top">تحميل تقرير الوكيل / المقاول :</td>
        <td><input type="file" name="agentReportUrl" id="agentReportUrl" multiple class=requiredFile /></td>
      </tr>
      <tr>
        <td class="side_label_middle">قطع الغيار المستخدمة :</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2"><table width="100%" id="jobOrderSpareParts">
          </table></td>
      </tr>
    </tbody>
    <tr>
      <td class="side_label_top">الإجراء الثاني :</td>
      <td><textarea name="secondAction" id="secondAction"></textarea></td>
    </tr>
    <tr>
      <td class="side_label_middle">تاريخ الإجراء الثاني :</td>
      <td><input type="text" name="secondActionDate" id="secondActionDate" class="caldr"></td>
    </tr>
    <tr>
      <td class="side_label_top">التقرير النهائي :</td>
      <td><textarea name="finalAction" id="finalAction"></textarea></td>
    </tr>
    <tr>
      <td class="side_label_middle">تاريخ التقرير النهائي :</td>
      <td><input type="text" name="finalActionDate" id="finalActionDate"  class="caldr"></td>
    </tr>
    <tr>
      <td class="side_label_top">رفع التقرير النهائي :</td>
      <td><input type="file" name="finalReportUrl" id="finalReportUrl" multiple></td>
    </tr>
    <% if(! WebUtil.sessionUserRestricted(request)){%>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2" class="side_label_middle" style="text-align:right"><input name="closed" id="closed" type="checkbox" value="1">
        تم الانتهاء من أمر العمل (سيتم إغلاق أمر العمل عند الضغط على هذا الاختيار)</td>
    </tr>
    <%}%>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="اتمام العملية"></td>
    </tr>
  </table>
</form>

<%
	JobOrder jobOrder = jobOrderInfo;
	int jobOrderSpareParts = 0 ;
	if (jobOrder.getJobOrderSpareParts()!= null )
	    jobOrderSpareParts = jobOrder.getJobOrderSpareParts().size();
    
    String firstAction = Common.getDisplayString(jobOrder.getFirstAction(),"");
    String firstActionDate =  DateUtil.getDateString(jobOrder.getFirstActionDate());
    Boolean fixIncludingSpareParts = jobOrder.getFixIncludingSpareParts();
    String secondAction = Common.getDisplayString(jobOrder.getSecondAction(),"");
    String secondActionDate =  DateUtil.getDateString(jobOrder.getSecondActionDate());
    String finalAction = Common.getDisplayString(jobOrder.getFinalAction(),"");
    String finalActionDate =  DateUtil.getDateString(jobOrder.getFinalActionDate());
    
    String agentReportFiles = jobOrder.getAgentReportUrl();
    String finalReportFiles =  jobOrder.getFinalReportUrl();
%>    
<script>
/*
function _onChangeRadio(){
	markAsNotRequired("#agentReportUrl");
	if($("input[name='fixIncludingSpareParts']:checked").val() == 1)
	   markAsRequired("#agentReportUrl");
}
*/

function _onBlurSecondAction(){

	if($("#secondAction").val().trim().length != 0 || $("#secondActionDate").val().trim().length != 0){
	    markAsRequired("#secondActionDate");
		markAsRequired("#secondAction");
	}else{
		markAsNotRequired("#secondAction");
		markAsNotRequired("#secondActionDate");
	}
}

function _onBlurThirdAction(){
	if($("#finalAction").val().trim().length != 0 || $("#finalActionDate").val().trim().length != 0 || $("#finalReportUrl").val().trim().length != 0){
	    markAsRequired("#secondActionDate");
		markAsRequired("#secondAction");
		markAsRequired("#finalAction");
		markAsRequired("#finalActionDate");
		markAsRequired("#finalReportUrl");
	}else{
		_onBlurSecondAction();
		markAsNotRequired("#finalAction");
		markAsNotRequired("#finalActionDate");
		markAsNotRequired("#finalReportUrl");
	}
}

$(function(){
	
	// validation
	$("input[name='fixIncludingSpareParts']").rules("add",{required:true});
	//$("input[name='fixIncludingSpareParts']").change(function(){_onChangeRadio()});
	
	$("#firstActionDate" ).rules( "add", {
		dateGreaterThan: ['#joborder_date'],
		messages: {	dateGreaterThan: "يجب أن يكون التاريخ بعد تاريخ فتح أمر العمل"}
	});
	
	$("#secondActionDate" ).rules( "add", {
		dateGreaterThan: ['#joborder_date',"#firstActionDate"],
		messages: {	dateGreaterThan: "يجب أن يكون التاريخ بعد  تاريخ الإجراء الأول"}
	});
	
	$("#finalActionDate" ).rules( "add", {
		dateGreaterThan: ['#joborder_date',"#firstActionDate","#secondActionDate"],
		messages: {	dateGreaterThan: "يجب أن يكون التاريخ بعد تاريخ الإجراء الثاني"}
	});
	

	// end of validation related code
	
	setRadioCheckedValue("fixIncludingSpareParts",'<%=(fixIncludingSpareParts != null && fixIncludingSpareParts) ? "1" : "0"%>');
	attachConditionalDisplayHandlerToRadio('fixIncludingSpareParts', {"1":"grp1"});
	
	$('#jobOrderSpareParts').appendGrid({
	    initRows: <%=jobOrderSpareParts%>,
	    i18n: {rowEmpty:'لم يتم إضافة أي ملحقات للجهاز'},
	    columns: [
	            { name: 'accQuantity', display: 'الكمية', type: 'text', displayCss:{'text-align':'center'}, ctrlCss: { display:'block', width: '50px', margin: 'auto', 'font-size':'.9em'}, ctrlProp: { required: true }, ctrlAttr: { 'data-rule-digits': true } },
	            { name: 'accDescription', display: 'الوصف' , type: 'text', displayCss:{'text-align':'center'}, ctrlCss: { display:'block', width: '250px', margin: 'auto', 'font-size':'.9em'}, ctrlProp: { required: true } },
	            { name: 'accPrice', display: 'سعر القطعة' , type: 'text', displayCss:{'text-align':'center'}, ctrlCss: { display:'block', width: '100px', margin: 'auto', 'font-size':'.9em'}, ctrlProp: { required: true }, ctrlAttr: { 'data-rule-number': true } },
	        ]
	}); 

   setInnerHTML("firstAction","<%=firstAction%>");
   setDateValue("firstActionDate","<%=firstActionDate%>");
         
         
  // setRadioCheckedValue("fixIncludingSpareParts",'<%=(fixIncludingSpareParts != null && fixIncludingSpareParts) ? "1" : "0"%>');
  //  $('input:radio[name="fixIncludingSpareParts"]:checked').change();
 
   <%  for (int i = 0 ; i < jobOrderSpareParts;  i++){ %>
            setTextFieldValue("jobOrderSpareParts_accQuantity_"+(<%=i+1%>),"<%=jobOrder.getJobOrderSpareParts().get(i).getQuantity()%>");
            setTextFieldValue("jobOrderSpareParts_accDescription_"+(<%=i+1%>),"<%=jobOrder.getJobOrderSpareParts().get(i).getDescription()%>");
            setTextFieldValue("jobOrderSpareParts_accPrice_"+(<%=i+1%>),"<%=jobOrder.getJobOrderSpareParts().get(i).getPrice()%>");
   <%}%>  
    
    setInnerHTML("secondAction","<%=secondAction%>");
    setDateValue("secondActionDate","<%=secondActionDate%>");
    setInnerHTML("finalAction","<%=finalAction%>");
    setDateValue("finalActionDate","<%=finalActionDate%>");
    
	showUploadedFilesList("agentReportUrl", <%=HtmlUtil.arrayFromJavaToJavaScript(agentReportFiles)%>);
	showUploadedFilesList("finalReportUrl", <%=HtmlUtil.arrayFromJavaToJavaScript(finalReportFiles)%>);
    
	<%String dest = request.getParameter(Default.SEARCH_DEST_PARAM_NAME);%>
	appendIdElement("<%=Default.SEARCH_DEST_PARAM_NAME%>","<%=dest%>");
	
	// validation
	$("#secondAction, #finalAction").blur(function(){_onBlurThirdAction()});
	$("#finalReportUrl").change(function(){_onBlurThirdAction()});
	$("#secondActionDate, #finalActionDate").datepicker( 'option' , 'onClose', function(){_onBlurThirdAction()});
	_onBlurThirdAction(); // To initiate the validation rules in the update mode
	// end of validation related code
    
});
</script>    

</body>
</html>
